package com.lwy.qiuchao.ware.service.impl;

import com.lwy.common.utils.R;
import com.lwy.qiuchao.ware.entity.BrandEntity;
import com.lwy.qiuchao.ware.service.BrandService;
import com.lwy.qiuchao.ware.vo.SkuDetailVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.common.utils.PageUtils;
import com.lwy.common.utils.Query;

import com.lwy.qiuchao.ware.dao.SkuInfoDao;
import com.lwy.qiuchao.ware.entity.SkuInfoEntity;
import com.lwy.qiuchao.ware.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("sku_id",key).or().like("sku_name",key);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public R deleteNumbyskuid(Long skuId, Integer skuCount) {
        Integer num = baseMapper.deleteNumbyskuid(skuId,skuCount);
        if(num!=0){
            return R.ok();
        }else {
            //TODO constant
            return R.error(1001,"库存不足");
        }

    }

    @Override
    public List<SkuInfoEntity> getList(List<Long> skuids) {
        return baseMapper.selectBatchIds(skuids);
    }

    @Override
    public R getinfo(Long skuId) {
        SkuDetailVo detailVo = new SkuDetailVo();
        SkuInfoEntity infoEntity = getById(skuId);
        if(infoEntity!=null){
            Long brandId = infoEntity.getBrandId();
            BrandEntity brandEntity = brandService.getById(brandId);
            BeanUtils.copyProperties(infoEntity,detailVo);
            detailVo.setBrandName(brandEntity.getName());
            return R.ok().put("data",detailVo);
        }else{
            //todo constant
            return R.error(10001,skuId+"号商品查询失败");
        }
    }

}